package steveshrader.budget.client;

import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;

import steveshrader.budget.client.widgets.Header;
import steveshrader.budget.shared.BudgetRequestFactory;

import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.core.client.GWT;
import com.google.gwt.core.client.GWT.UncaughtExceptionHandler;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.shared.EventBus;
import com.google.gwt.event.shared.SimpleEventBus;
import com.google.gwt.resources.client.ClientBundle;
import com.google.gwt.resources.client.CssResource;
import com.google.gwt.resources.client.CssResource.NotStrict;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.uibinder.client.UiHandler;
import com.google.gwt.user.client.ui.LayoutPanel;
import com.google.gwt.user.client.ui.RootLayoutPanel;
import com.google.gwt.user.client.ui.ToggleButton;
import com.google.gwt.user.client.ui.Widget;
import com.google.web.bindery.requestfactory.gwt.client.RequestFactoryLogHandler;
import com.google.web.bindery.requestfactory.shared.LoggingRequest;

/**
 * The entry point class which performs the initial loading of the Budget application.
 */
public class Budget implements EntryPoint {
  interface Binder extends UiBinder<Widget, Budget> { }

  interface GlobalResources extends ClientBundle {
	@NotStrict
	@Source("global.css")
	CssResource css();
  }

  private static final Logger log = Logger.getLogger(Budget.class.getName());

  @UiField(provided = true)
  Header header;
  
  @UiField
  LayoutPanel container;
  
  @UiField
  ToggleButton expensesButton;

  @UiField
  ToggleButton chartsButton;

  @UiField(provided = true)
  ExpensesLayout expensesLayout;

  ChartsLayout chartsLayout;
  
  EventBus eventBus = new SimpleEventBus();
  BudgetRequestFactory requests;
  FieldsHelper fieldsHelper;


  /**
   * This method sets up the top-level services used by the application.
   */
  public void onModuleLoad() {
    GWT.setUncaughtExceptionHandler(new UncaughtExceptionHandler() {
      public void onUncaughtException(Throwable e) {
        log.log(Level.SEVERE, e.getMessage(), e);
      }
    });

    // Inject global styles.
    GWT.<GlobalResources>create(GlobalResources.class).css().ensureInjected();

    requests = GWT.create(BudgetRequestFactory.class);
    requests.initialize(eventBus);

    fieldsHelper = new FieldsHelper(requests);	//common services used by other components
    
    // Add remote logging handler
    RequestFactoryLogHandler.LoggingRequestProvider provider = new RequestFactoryLogHandler.LoggingRequestProvider() {
      public LoggingRequest getLoggingRequest() {
        return requests.loggingRequest();
      }
    };
    Logger.getLogger("").addHandler(new ErrorDialog().getHandler());
    Logger.getLogger("").addHandler(
        new RequestFactoryLogHandler(provider, Level.WARNING,
            new ArrayList<String>()));
    ExpenseEditorWorkflow.register(eventBus, requests, fieldsHelper);

    header = new Header(requests);
    expensesLayout = new ExpensesLayout(eventBus, requests, fieldsHelper);
    chartsLayout = new ChartsLayout(eventBus, requests, fieldsHelper);
    
    RootLayoutPanel.get().add(GWT.<Binder> create(Binder.class).createAndBindUi(this));
  }
  
  @UiHandler("expensesButton")
  public void handleExpensesButtonClick(ClickEvent e) {
	  container.clear();
	  chartsButton.setDown(false);
	  expensesButton.setDown(true);
	  container.add(expensesLayout);
  }
  
  @UiHandler("chartsButton")
  public void handleChartsButtonClick(ClickEvent e) {
	  container.clear();
	  expensesButton.setDown(false);
	  chartsButton.setDown(true);
	  container.add(chartsLayout);
  }
}
